﻿using apitest.Application.Entities.Pms;

namespace apitest.Application.Test.Services
{
    [DynamicApiController]
    public class Test9Service
    {

        ISqlSugarClient _db;
        List<string> useBatch;
        List<Production_Intermediate> allIntermediaList;
        List<Production_Record_Intermediate> allRecordIntermediateList;
        List<Production_Batch> allBatchList;
        public Test9Service(ISqlSugarClient db)
        {
            _db = db;
        }
        [HttpGet, QueryParameters]
        public async Task<dynamic> T1()
        {
            useBatch = new List<string>();
            allIntermediaList = await _db.Queryable<Production_Intermediate>().ToListAsync();
            allRecordIntermediateList = await _db.Queryable<Production_Record_Intermediate>()
              .ToListAsync();
            allBatchList = await _db.Queryable<Production_Batch>()
              // .Where(o => o.Batch.StartsWith("7"))
              .ToListAsync();
            foreach (var item in allBatchList)
            {
                AddUseList(allBatchList.Select(o => o.Batch).ToList());
            }
            useBatch = useBatch.Distinct().ToList();
            allIntermediaList = allIntermediaList.Where(o => useBatch.Contains(o.Code)).ToList();

            //置0
            foreach (var item in allIntermediaList)
            {
                item.COGAmount = 0;
                item.COGPrice = 0;
                item.Price = 0;
                item.Amount = 0;
            }

            foreach (var item in allBatchList)
            {
                item.COGPrice = 0;
                item.Amount = 0;
                item.IsAmount = false;
            }

            //获取所有没有领用中间体的批次
            var batchs = allRecordIntermediateList.Select(o => o.Batch).ToList();
            var notUseList = allIntermediaList.Where(o => !batchs.Contains(o.Code)).ToList();
            foreach (var item in notUseList)
            {
                ComputedCost(item.Code);
            }
            SearchNoComputedBatch();
            return allBatchList;
        }

        //获取所有关联批次
        void AddUseList(List<string> batchs )
        {
            var list = allRecordIntermediateList.Where(o => batchs.Contains(o.Batch)).ToList();
            if (list.Count > 0)
            {
                var b = list.Select(o => o.IntermediateCode).ToList();
                useBatch.AddRange(b);
                AddUseList(b );
            }
        }

        //获取所有没有领用中间体的批次
        void GetAllNoUseBatch()
        {
            var batchs = allRecordIntermediateList.Select(o => o.Batch).ToList();
            var notUseList = allIntermediaList.Where(o => !batchs.Contains(o.Code)).ToList();
        }

        //计算批次成本
        void ComputedCost(string batch)
        {
            var batchData = allBatchList.FirstOrDefault(o => o.Batch == batch);
            var useList = allRecordIntermediateList.Where(o => o.Batch == batch).ToList();
            var IntermediateAmount = 0.0;
            var COGAmount = 0.0;
            foreach (var item in useList)
            {
                var a = allIntermediaList.FirstOrDefault(o => o.Code == item.IntermediateCode);
                if (a!=null)
                {
                    IntermediateAmount += item.Num * a.Price;
                    COGAmount += item.Num * a.COGPrice;
                }
            }
            batchData.IntermediateAmount =Math.Round( IntermediateAmount,2);
            var amount = batchData.DeviceAmount + batchData.UserAmount + batchData.MaterialAmount
                + batchData.HeightWastesLiquidAmount + batchData.LowWastesLiquidAmount
                + batchData.WastesLiquidAmount + batchData.WastesSolidAmount + batchData.IntermediateAmount;
            batchData.Amount = Math.Round(amount, 2);
            batchData.COGPrice = batchData.ProduceNum == 0 ? 0
                    : Math.Round((batchData.MaterialAmount + COGAmount) / batchData.ProduceNum.Value, 2);
            batchData.IsAmount = true;
            var intermedia = allIntermediaList.FirstOrDefault(o => o.Code == batch);
            if (intermedia != null)
            {
                intermedia.Price = batchData.ProduceNum == 0 || batchData.Amount == 0 ? 0
              : Math.Round(batchData.Amount / batchData.ProduceNum.Value, 2);
                intermedia.COGPrice = batchData.COGPrice;
            }
        }

        //递归查找未计算成本的批次
        void SearchNoComputedBatch()
        {
            var list = allBatchList.Where(o =>!o.IsAmount).ToList();
            if (list.Count > 0) {
                foreach (var item in list)
                {
                    var useList = allRecordIntermediateList.Where(o => o.Batch == item.Batch).ToList();
                    var flag = true;
                    foreach (var item1 in useList)
                    {
                        var a = allBatchList.FirstOrDefault(o => o.Batch == item1.IntermediateCode);
                        if ( !a.IsAmount) {
                            flag = false;
                        }
                    }
                    if (flag)
                    {
                        ComputedCost(item.Batch);
                    }
                }
                SearchNoComputedBatch();
            }
        }

    }
}
